using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;

namespace ASPNETVN.PORTAL.Modules.ACTP.Components
{
	public partial class Task
	{
		#region Properties.
		
		public int TaskID { set; get; }
		public Guid ModuleID { set; get; }
		public int TaskTypeID { set; get; }
		public string Subject { set; get; }
		public DateTime StartDate { set; get; }
		public DateTime DueDate { set; get; }
		public DateTime CompletedDate { set; get; }
		public string Status { set; get; }
		public string Priority { set; get; }
		public byte CompletedPercent { set; get; }
		public bool IsPrivate { set; get; }
		public bool IsCompleted { set; get; }
		public bool IsApproved { set; get; }
		public string Description { set; get; }
		public string CreatedBy { set; get; }
		public DateTime CreatedDate { set; get; }
		public string UpdatedBy { set; get; }
		public DateTime UpdatedDate { set; get; }
		public string ApprovedBy { set; get; }
		public DateTime ApprovedDate { set; get; }
		
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Methods
		protected static IList<Task> ConvertToCollection(IDataReader reader)
		{
			IList<Task> collection = new List<Task>();
			while (reader.Read())
			{
				Task entity = new Task();
				if (!reader.IsDBNull(reader.GetOrdinal("TaskID"))) entity.TaskID = reader.GetInt32(reader.GetOrdinal("TaskID"));
				if (!reader.IsDBNull(reader.GetOrdinal("ModuleID"))) entity.ModuleID = reader.GetGuid(reader.GetOrdinal("ModuleID"));
				if (!reader.IsDBNull(reader.GetOrdinal("TaskTypeID"))) entity.TaskTypeID = reader.GetInt32(reader.GetOrdinal("TaskTypeID"));
				if (!reader.IsDBNull(reader.GetOrdinal("Subject"))) entity.Subject = reader.GetString(reader.GetOrdinal("Subject"));
				if (!reader.IsDBNull(reader.GetOrdinal("StartDate"))) entity.StartDate = reader.GetDateTime(reader.GetOrdinal("StartDate"));
				if (!reader.IsDBNull(reader.GetOrdinal("DueDate"))) entity.DueDate = reader.GetDateTime(reader.GetOrdinal("DueDate"));
				if (!reader.IsDBNull(reader.GetOrdinal("CompletedDate"))) entity.CompletedDate = reader.GetDateTime(reader.GetOrdinal("CompletedDate"));
				if (!reader.IsDBNull(reader.GetOrdinal("Status"))) entity.Status = reader.GetString(reader.GetOrdinal("Status"));
				if (!reader.IsDBNull(reader.GetOrdinal("Priority"))) entity.Priority = reader.GetString(reader.GetOrdinal("Priority"));
				if (!reader.IsDBNull(reader.GetOrdinal("CompletedPercent"))) entity.CompletedPercent = reader.GetByte(reader.GetOrdinal("CompletedPercent"));
				if (!reader.IsDBNull(reader.GetOrdinal("IsPrivate"))) entity.IsPrivate = reader.GetBoolean(reader.GetOrdinal("IsPrivate"));
				if (!reader.IsDBNull(reader.GetOrdinal("IsCompleted"))) entity.IsCompleted = reader.GetBoolean(reader.GetOrdinal("IsCompleted"));
				if (!reader.IsDBNull(reader.GetOrdinal("IsApproved"))) entity.IsApproved = reader.GetBoolean(reader.GetOrdinal("IsApproved"));
				if (!reader.IsDBNull(reader.GetOrdinal("Description"))) entity.Description = reader.GetString(reader.GetOrdinal("Description"));
				if (!reader.IsDBNull(reader.GetOrdinal("CreatedBy"))) entity.CreatedBy = reader.GetString(reader.GetOrdinal("CreatedBy"));
				if (!reader.IsDBNull(reader.GetOrdinal("CreatedDate"))) entity.CreatedDate = reader.GetDateTime(reader.GetOrdinal("CreatedDate"));
				if (!reader.IsDBNull(reader.GetOrdinal("UpdatedBy"))) entity.UpdatedBy = reader.GetString(reader.GetOrdinal("UpdatedBy"));
				if (!reader.IsDBNull(reader.GetOrdinal("UpdatedDate"))) entity.UpdatedDate = reader.GetDateTime(reader.GetOrdinal("UpdatedDate"));
				if (!reader.IsDBNull(reader.GetOrdinal("ApprovedBy"))) entity.ApprovedBy = reader.GetString(reader.GetOrdinal("ApprovedBy"));
				if (!reader.IsDBNull(reader.GetOrdinal("ApprovedDate"))) entity.ApprovedDate = reader.GetDateTime(reader.GetOrdinal("ApprovedDate"));
				collection.Add(entity);
			}
			reader.Close();
			return collection;
		}
		
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Select methods.
		
		public static Task Load(int taskID)
		{
			const string spName = "[dbo].[p_ACTP_Task_Load]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			
			db.AddInParameter(dbCommand, "@TaskID", SqlDbType.Int, taskID);
            IDataReader reader = db.ExecuteReader(dbCommand);
			IList<Task> collection = ConvertToCollection(reader);	
			if (collection.Count > 0)
			{
				return collection[0];
			}
			return null;
		}		
		
		//---------------------------------------------------------------------------------------------
		public static IList<Task> SelectCollectionAll(Guid moduleID)
		{
			IDataReader reader = SelectReaderAll(moduleID);
			return ConvertToCollection(reader);			
		}		
		
		//---------------------------------------------------------------------------------------------
		
		public static IList<Task> SelectCollectionDynamic(string whereCondition, string orderByExpression)
		{
			IDataReader reader = SelectReaderDynamic(whereCondition, orderByExpression);
			return ConvertToCollection(reader);		
		}
		
		//---------------------------------------------------------------------------------------------
		
		// Select by foreign key return collection		
		public static IList<Task> SelectCollectionBy_TaskTypeID(int taskTypeID)
		{
            IDataReader reader = SelectReaderBy_TaskTypeID(taskTypeID);
			return ConvertToCollection(reader);	
		}		
		//---------------------------------------------------------------------------------------------
		
		public static DataSet SelectBy_TaskTypeID(int taskTypeID)
		{
			const string spName = "[dbo].[p_ACTP_Task_SelectBy_TaskTypeID]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			
			db.AddInParameter(dbCommand, "@TaskTypeID", SqlDbType.Int, taskTypeID);
						
            return db.ExecuteDataSet(dbCommand);
		}
		//---------------------------------------------------------------------------------------------

		public static DataSet SelectAll(Guid moduleID)
        {
            const string spName = "[dbo].[p_ACTP_Task_SelectAll]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@ModuleID", SqlDbType.UniqueIdentifier, moduleID);
            return db.ExecuteDataSet(dbCommand);
        }
		
		//---------------------------------------------------------------------------------------------
		
		public static DataSet SelectDynamic(string whereCondition, string orderByExpression)
		{
            const string spName = "[dbo].[p_ACTP_Task_SelectDynamic]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            db.AddInParameter(dbCommand, "@OrderByExpression", SqlDbType.NVarChar, orderByExpression);
            
            return db.ExecuteDataSet(dbCommand);        				
		}
		
		//---------------------------------------------------------------------------------------------
				
		public static IDataReader SelectReaderAll(Guid moduleID)
        {
            const string spName = "[dbo].[p_ACTP_Task_SelectAll]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			db.AddInParameter(dbCommand, "@ModuleID", SqlDbType.UniqueIdentifier, moduleID);
            return db.ExecuteReader(dbCommand);
        }
		
		//---------------------------------------------------------------------------------------------
		
		public static IDataReader SelectReaderDynamic(string whereCondition, string orderByExpression)
		{
            const string spName = "[dbo].[p_ACTP_Task_SelectDynamic]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            db.AddInParameter(dbCommand, "@OrderByExpression", SqlDbType.NVarChar, orderByExpression);
            
            return db.ExecuteReader(dbCommand);        				
		}
		
		//---------------------------------------------------------------------------------------------
		
		// Select by foreign key return collection		
		public static IDataReader SelectReaderBy_TaskTypeID(int taskTypeID)
		{
			const string spName = "p_ACTP_Task_SelectBy_TaskTypeID";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			
			db.AddInParameter(dbCommand, "@TaskTypeID", SqlDbType.Int, taskTypeID);
			
            return db.ExecuteReader(dbCommand);
		}		
		//---------------------------------------------------------------------------------------------
		
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Insert methods.
		
		public static int InsertTask(Guid moduleID, int taskTypeID, string subject, DateTime startDate, DateTime dueDate, DateTime completedDate, string status, string priority, byte completedPercent, bool isPrivate, bool isCompleted, bool isApproved, string description, string createdBy, DateTime createdDate, string updatedBy, DateTime updatedDate, string approvedBy, DateTime approvedDate)
		{
			Task entity = new Task();	
			entity.ModuleID = moduleID;
			entity.TaskTypeID = taskTypeID;
			entity.Subject = subject;
			entity.StartDate = startDate;
			entity.DueDate = dueDate;
			entity.CompletedDate = completedDate;
			entity.Status = status;
			entity.Priority = priority;
			entity.CompletedPercent = completedPercent;
			entity.IsPrivate = isPrivate;
			entity.IsCompleted = isCompleted;
			entity.IsApproved = isApproved;
			entity.Description = description;
			entity.CreatedBy = createdBy;
			entity.CreatedDate = createdDate;
			entity.UpdatedBy = updatedBy;
			entity.UpdatedDate = updatedDate;
			entity.ApprovedBy = approvedBy;
			entity.ApprovedDate = approvedDate;
			return entity.Insert();
		}
		
		public int Insert()
		{
			return this.Insert(null);
		}		
		
		//---------------------------------------------------------------------------------------------
		
		public int Insert(SqlTransaction transaction)
		{			
			const string spName = "[dbo].[p_ACTP_Task_Insert]";		
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddOutParameter(dbCommand, "@TaskID", SqlDbType.Int, 4);
			db.AddInParameter(dbCommand, "@ModuleID", SqlDbType.UniqueIdentifier, ModuleID);
			db.AddInParameter(dbCommand, "@TaskTypeID", SqlDbType.Int, TaskTypeID);
			db.AddInParameter(dbCommand, "@Subject", SqlDbType.NVarChar, Subject);
			db.AddInParameter(dbCommand, "@StartDate", SqlDbType.DateTime, StartDate.Year <= 1753 ? DBNull.Value : (object) StartDate);
			db.AddInParameter(dbCommand, "@DueDate", SqlDbType.DateTime, DueDate.Year <= 1753 ? DBNull.Value : (object) DueDate);
			db.AddInParameter(dbCommand, "@CompletedDate", SqlDbType.DateTime, CompletedDate.Year <= 1753 ? DBNull.Value : (object) CompletedDate);
			db.AddInParameter(dbCommand, "@Status", SqlDbType.VarChar, Status);
			db.AddInParameter(dbCommand, "@Priority", SqlDbType.Char, Priority);
			db.AddInParameter(dbCommand, "@CompletedPercent", SqlDbType.TinyInt, CompletedPercent);
			db.AddInParameter(dbCommand, "@IsPrivate", SqlDbType.Bit, IsPrivate);
			db.AddInParameter(dbCommand, "@IsCompleted", SqlDbType.Bit, IsCompleted);
			db.AddInParameter(dbCommand, "@IsApproved", SqlDbType.Bit, IsApproved);
			db.AddInParameter(dbCommand, "@Description", SqlDbType.NVarChar, Description);
			db.AddInParameter(dbCommand, "@CreatedBy", SqlDbType.VarChar, CreatedBy);
			db.AddInParameter(dbCommand, "@CreatedDate", SqlDbType.DateTime, CreatedDate.Year <= 1753 ? DBNull.Value : (object) CreatedDate);
			db.AddInParameter(dbCommand, "@UpdatedBy", SqlDbType.VarChar, UpdatedBy);
			db.AddInParameter(dbCommand, "@UpdatedDate", SqlDbType.DateTime, UpdatedDate.Year <= 1753 ? DBNull.Value : (object) UpdatedDate);
			db.AddInParameter(dbCommand, "@ApprovedBy", SqlDbType.VarChar, ApprovedBy);
			db.AddInParameter(dbCommand, "@ApprovedDate", SqlDbType.DateTime, ApprovedDate.Year <= 1753 ? DBNull.Value : (object) ApprovedDate);
			
			if (transaction != null)
			{
				db.ExecuteNonQuery(dbCommand, transaction);
				TaskID = (int) db.GetParameterValue(dbCommand, "@TaskID");
				return TaskID;
			}
            else
			{
				db.ExecuteNonQuery(dbCommand);
				TaskID = (int) db.GetParameterValue(dbCommand, "@TaskID");
				return TaskID;
			}			
		}
				
		//---------------------------------------------------------------------------------------------
		public static bool InsertCollection(IList<Task> collection)
        {
            bool ret;
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                using(SqlTransaction transaction = connection.BeginTransaction())
				{
					try
					{
						bool ret01 = true;
						foreach (Task item in collection)
						{
							if (item.Insert(transaction) <= 0)
							{							
								ret01 = false;
								break;
							}
						}
						if (ret01)
						{
							transaction.Commit();
							ret = true;
						}
						else
						{
							transaction.Rollback();
							ret = false;                    	
						}
					}
					catch (Exception ex)
					{
						transaction.Rollback();
						throw new Exception("Error at InsertCollection method: " + ex.Message);
					}
					finally 
					{
						connection.Close();
					}
				}
            }
            return ret;		
		}
		
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Insert / Update methods.
		
		public static int InsertUpdateTask(int taskID, Guid moduleID, int taskTypeID, string subject, DateTime startDate, DateTime dueDate, DateTime completedDate, string status, string priority, byte completedPercent, bool isPrivate, bool isCompleted, bool isApproved, string description, string createdBy, DateTime createdDate, string updatedBy, DateTime updatedDate, string approvedBy, DateTime approvedDate)
		{
			Task entity = new Task();			
			entity.TaskID = taskID;
			entity.ModuleID = moduleID;
			entity.TaskTypeID = taskTypeID;
			entity.Subject = subject;
			entity.StartDate = startDate;
			entity.DueDate = dueDate;
			entity.CompletedDate = completedDate;
			entity.Status = status;
			entity.Priority = priority;
			entity.CompletedPercent = completedPercent;
			entity.IsPrivate = isPrivate;
			entity.IsCompleted = isCompleted;
			entity.IsApproved = isApproved;
			entity.Description = description;
			entity.CreatedBy = createdBy;
			entity.CreatedDate = createdDate;
			entity.UpdatedBy = updatedBy;
			entity.UpdatedDate = updatedDate;
			entity.ApprovedBy = approvedBy;
			entity.ApprovedDate = approvedDate;
			return entity.InsertUpdate();
		}
		
		//---------------------------------------------------------------------------------------------
		
		public int InsertUpdate()
		{
			return this.InsertUpdate(null);
		}
		
		//---------------------------------------------------------------------------------------------

		public int InsertUpdate(SqlTransaction transaction)
		{			
			const string spName = "p_ACTP_Task_InsertUpdate";		
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@TaskID", SqlDbType.Int, TaskID);
			db.AddInParameter(dbCommand, "@ModuleID", SqlDbType.UniqueIdentifier, ModuleID);
			db.AddInParameter(dbCommand, "@TaskTypeID", SqlDbType.Int, TaskTypeID);
			db.AddInParameter(dbCommand, "@Subject", SqlDbType.NVarChar, Subject);
			db.AddInParameter(dbCommand, "@StartDate", SqlDbType.DateTime, StartDate.Year <= 1753 ? DBNull.Value : (object) StartDate);
			db.AddInParameter(dbCommand, "@DueDate", SqlDbType.DateTime, DueDate.Year <= 1753 ? DBNull.Value : (object) DueDate);
			db.AddInParameter(dbCommand, "@CompletedDate", SqlDbType.DateTime, CompletedDate.Year <= 1753 ? DBNull.Value : (object) CompletedDate);
			db.AddInParameter(dbCommand, "@Status", SqlDbType.VarChar, Status);
			db.AddInParameter(dbCommand, "@Priority", SqlDbType.Char, Priority);
			db.AddInParameter(dbCommand, "@CompletedPercent", SqlDbType.TinyInt, CompletedPercent);
			db.AddInParameter(dbCommand, "@IsPrivate", SqlDbType.Bit, IsPrivate);
			db.AddInParameter(dbCommand, "@IsCompleted", SqlDbType.Bit, IsCompleted);
			db.AddInParameter(dbCommand, "@IsApproved", SqlDbType.Bit, IsApproved);
			db.AddInParameter(dbCommand, "@Description", SqlDbType.NVarChar, Description);
			db.AddInParameter(dbCommand, "@CreatedBy", SqlDbType.VarChar, CreatedBy);
			db.AddInParameter(dbCommand, "@CreatedDate", SqlDbType.DateTime, CreatedDate.Year <= 1753 ? DBNull.Value : (object) CreatedDate);
			db.AddInParameter(dbCommand, "@UpdatedBy", SqlDbType.VarChar, UpdatedBy);
			db.AddInParameter(dbCommand, "@UpdatedDate", SqlDbType.DateTime, UpdatedDate.Year <= 1753 ? DBNull.Value : (object) UpdatedDate);
			db.AddInParameter(dbCommand, "@ApprovedBy", SqlDbType.VarChar, ApprovedBy);
			db.AddInParameter(dbCommand, "@ApprovedDate", SqlDbType.DateTime, ApprovedDate.Year <= 1753 ? DBNull.Value : (object) ApprovedDate);
			
			if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);			
		}
		
		//---------------------------------------------------------------------------------------------
		public static bool InsertUpdateCollection(IList<Task> collection)
        {
            bool ret;
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
				{
					try
					{
						bool ret01 = true;
						foreach (Task item in collection)
						{
							if (item.InsertUpdate(transaction) <= 0)
							{
								ret01 = false;
								break;
							}
						}
						if (ret01)
						{
							transaction.Commit();
							ret = true;
						}
						else
						{
							transaction.Rollback();
							ret = false;                    	
						}
					}
					catch (Exception ex)
					{
						transaction.Rollback();
						throw new Exception("Error at InsertUpdateCollection method: " + ex.Message);
						
					}
					finally 
					{
						connection.Close();
					}
				}
            }
            return ret;		
		}	
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Update methods.
		
		public static int UpdateTask(int taskID, Guid moduleID, int taskTypeID, string subject, DateTime startDate, DateTime dueDate, DateTime completedDate, string status, string priority, byte completedPercent, bool isPrivate, bool isCompleted, bool isApproved, string description, string createdBy, DateTime createdDate, string updatedBy, DateTime updatedDate, string approvedBy, DateTime approvedDate)
		{
			Task entity = new Task();			
			entity.TaskID = taskID;
			entity.ModuleID = moduleID;
			entity.TaskTypeID = taskTypeID;
			entity.Subject = subject;
			entity.StartDate = startDate;
			entity.DueDate = dueDate;
			entity.CompletedDate = completedDate;
			entity.Status = status;
			entity.Priority = priority;
			entity.CompletedPercent = completedPercent;
			entity.IsPrivate = isPrivate;
			entity.IsCompleted = isCompleted;
			entity.IsApproved = isApproved;
			entity.Description = description;
			entity.CreatedBy = createdBy;
			entity.CreatedDate = createdDate;
			entity.UpdatedBy = updatedBy;
			entity.UpdatedDate = updatedDate;
			entity.ApprovedBy = approvedBy;
			entity.ApprovedDate = approvedDate;
			return entity.Update();
		}
		
		//---------------------------------------------------------------------------------------------
		
		public int Update()
		{
			return this.Update(null);
		}
		
		//---------------------------------------------------------------------------------------------

		public int Update(SqlTransaction transaction)
		{
			const string spName = "[dbo].[p_ACTP_Task_Update]";		
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@TaskID", SqlDbType.Int, TaskID);
			db.AddInParameter(dbCommand, "@ModuleID", SqlDbType.UniqueIdentifier, ModuleID);
			db.AddInParameter(dbCommand, "@TaskTypeID", SqlDbType.Int, TaskTypeID);
			db.AddInParameter(dbCommand, "@Subject", SqlDbType.NVarChar, Subject);
			db.AddInParameter(dbCommand, "@StartDate", SqlDbType.DateTime, StartDate.Year <= 1753 ? DBNull.Value : (object) StartDate);
			db.AddInParameter(dbCommand, "@DueDate", SqlDbType.DateTime, DueDate.Year <= 1753 ? DBNull.Value : (object) DueDate);
			db.AddInParameter(dbCommand, "@CompletedDate", SqlDbType.DateTime, CompletedDate.Year <= 1753 ? DBNull.Value : (object) CompletedDate);
			db.AddInParameter(dbCommand, "@Status", SqlDbType.VarChar, Status);
			db.AddInParameter(dbCommand, "@Priority", SqlDbType.Char, Priority);
			db.AddInParameter(dbCommand, "@CompletedPercent", SqlDbType.TinyInt, CompletedPercent);
			db.AddInParameter(dbCommand, "@IsPrivate", SqlDbType.Bit, IsPrivate);
			db.AddInParameter(dbCommand, "@IsCompleted", SqlDbType.Bit, IsCompleted);
			db.AddInParameter(dbCommand, "@IsApproved", SqlDbType.Bit, IsApproved);
			db.AddInParameter(dbCommand, "@Description", SqlDbType.NVarChar, Description);
			db.AddInParameter(dbCommand, "@CreatedBy", SqlDbType.VarChar, CreatedBy);
			db.AddInParameter(dbCommand, "@CreatedDate", SqlDbType.DateTime, CreatedDate.Year <= 1753 ? DBNull.Value : (object) CreatedDate);
			db.AddInParameter(dbCommand, "@UpdatedBy", SqlDbType.VarChar, UpdatedBy);
			db.AddInParameter(dbCommand, "@UpdatedDate", SqlDbType.DateTime, UpdatedDate.Year <= 1753 ? DBNull.Value : (object) UpdatedDate);
			db.AddInParameter(dbCommand, "@ApprovedBy", SqlDbType.VarChar, ApprovedBy);
			db.AddInParameter(dbCommand, "@ApprovedDate", SqlDbType.DateTime, ApprovedDate.Year <= 1753 ? DBNull.Value : (object) ApprovedDate);
			
			if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);
		}
				
		//---------------------------------------------------------------------------------------------
		public static bool UpdateCollection(IList<Task> collection)
        {
            bool ret;
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
				{
					try
					{
						bool ret01 = true;
						foreach (Task item in collection)
						{
							if (item.Update(transaction) <= 0)
							{
								ret01 = false;
								break;
							}
						}
						if (ret01)
						{
							transaction.Commit();
							ret = true;
						}
						else
						{
							transaction.Rollback();
							ret = false;                    	
						}
					}
					catch (Exception ex)
					{
						transaction.Rollback();
						throw new Exception("Error at UpdateCollection method: " + ex.Message);
					}
					finally 
					{
						connection.Close();
					}
				}
            }
            return ret;		
		}
		
		#endregion
		
		//---------------------------------------------------------------------------------------------
		
		#region Delete methods.
		
		public static int DeleteTask(int taskID)
		{
			Task entity = new Task();
			entity.TaskID = taskID;
			
			return entity.Delete();
		}
		
		public int Delete()
		{
			return this.Delete(null);
		}
		
		//---------------------------------------------------------------------------------------------

		public int Delete(SqlTransaction transaction)
		{
			const string spName = "[dbo].[p_ACTP_Task_Delete]";		
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
			SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			
			db.AddInParameter(dbCommand, "@TaskID", SqlDbType.Int, TaskID);
			
			if (transaction != null)
                return db.ExecuteNonQuery(dbCommand, transaction);
            else
                return db.ExecuteNonQuery(dbCommand);
		}
		
		//---------------------------------------------------------------------------------------------
		
		public static int DeleteBy_TaskTypeID(int taskTypeID)
		{
			const string spName = "[dbo].[p_ACTP_Task_DeleteBy_TaskTypeID]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);
			
			db.AddInParameter(dbCommand, "@TaskTypeID", SqlDbType.Int, taskTypeID);
						
            return db.ExecuteNonQuery(dbCommand);
		}
		
		//---------------------------------------------------------------------------------------------
			
		
		public static int DeleteDynamic(string whereCondition)
		{
			const string spName = "[dbo].[p_ACTP_Task_DeleteDynamic]";
            SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            SqlCommand dbCommand = (SqlCommand) db.GetStoredProcCommand(spName);

			db.AddInParameter(dbCommand, "@WhereCondition", SqlDbType.NVarChar, whereCondition);
            
            return db.ExecuteNonQuery(dbCommand);   
		}
		//---------------------------------------------------------------------------------------------
		
		public static bool DeleteCollection(IList<Task> collection)
        {
            bool ret;
			SqlDatabase db = (SqlDatabase) DatabaseFactory.CreateDatabase();
            using (SqlConnection connection = (SqlConnection)db.CreateConnection())
            {
                connection.Open();
                using (SqlTransaction transaction = connection.BeginTransaction())
				{
					try
					{
						bool ret01 = true;
						foreach (Task item in collection)
						{
							if (item.Delete(transaction) <= 0)
							{
								ret01 = false;
								break;
							}
						}
						if (ret01)
						{
							transaction.Commit();
							ret = true;
						}
						else
						{
							transaction.Rollback();
							ret = false;                    	
						}
					}
					catch (Exception ex)
					{
						transaction.Rollback();
						throw new Exception("Error at DeleteCollection method: " + ex.Message);
					}
					finally 
					{
						connection.Close();
					}
				}
            }
            return ret;		
		}
		#endregion
	}	
}